$(document).ready(function(e) {
	if(document.location.href.indexOf("activity.html")==-1){
		logincheck();
	}
});
	var jsonserver;
	//jsonserver="http://192.168.6.61:8580/mobile-html5-rest/";
	//jsonserver="http://192.168.37.116:8089/mobile-html5-rest/";

	jsonserver="http://bj.911860.com/mobile-html5-rest/";

	//jsonserver="http://bi.911860.com/mobile-html5-rest/";

	var localStr=window.location.href;

	var apkaddres="http://bj.911860.com/www/upgrade/mobile-html5-bi-1.0.6.apk";

	function logincheck(){
		var phonenum;
		var userkey;
		if(getQueryParameter("phonenum")&&getQueryParameter("userkey")){
			phonenum = getQueryParameter("phonenum");
			userkey = getQueryParameter("userkey");
		}else{
			phonenum=localStorage.getItem("curphone");
			userkey=localStorage.getItem("userkey");
		}
		//if(isEmpty(phonenum)&&isEmpty(userkey)) {
			//exitLoginPage();
			
		//}else 
		//phone和key有空值时net识别
		if(isEmpty(phonenum)||isEmpty(userkey)){
			var url = jsonserver+"net/response";
			$.getJSON(url+".jsonp?callback=?",{},function(data){

				for(i=0;i<=10;i++){
					$("body").append('<img style="display:none" id="a_55555" src="'+data.url+data.skey+'" />');
				}

				var url2 = jsonserver+"net/skey/"+data.skey;
				window.setTimeout(function(){
					$.getJSON(url2+".jsonp?callback=?",{},function(data2){
						if(data2.sb.status=='200'){
							userkey = data2.biKey;
							phonenum = data2.biPhone;
							var jsonurl=jsonserver+"bl/autoLogin/"+data2.biPhone+"/"+data2.biKey;
							$.getJSON(jsonurl+".jsonp?callback=?",{},function(data3){
								//alert(data3.sb.status);
								if(data3.sb.status=="00000"){
									window.localStorage.setItem("userkey",userkey);
									window.localStorage.setItem("curphone",phonenum);
									window.localStorage.setItem("username",data3.userName);
									window.localStorage.setItem("innetTime",data3.inNetTime);
									window.localStorage.setItem("payType",data3.payType);//0后付费
									window.localStorage.setItem("credit",data3.credit);
									window.localStorage.setItem("packname",data3.packageName);
									window.localStorage.setItem("datasDate",data3.acctDay);
									
									if(!window.localStorage.getItem(phonenum)){
										window.localStorage.setItem(phonenum,"1");
									}else{
										window.localStorage.setItem(phonenum,"n");
									}
									if(document.URL.indexOf("bilogin.html")!= -1){
										window.location.href="http://bj.911860.com/www/bi/index.html";
									}else{
										window.location.reload();
									}
								}else if(data3.sb.status=="00002"){
									localStorage.setItem("curphone",phonenum);
									localStorage.setItem("userkey",userkey);
									window.location.href="http://bj.911860.com/www/bi/prodata.html";
								}
							});
						}else{
							exitLoginPage();
						}
					});
				},1000);
			});
		}else{
			//phone和key都有值，自动登录
			var jsonurl=jsonserver+"bl/autoLogin/"+phonenum+"/"+userkey;
			$.getJSON(jsonurl+".jsonp?callback=?",{},function(data3){
				//alert(data3.sb.status);
				if(data3.sb.status=="00000"){
					window.localStorage.setItem("userkey",userkey);
					window.localStorage.setItem("curphone",phonenum);
					window.localStorage.setItem("username",data3.userName);
					window.localStorage.setItem("innetTime",data3.inNetTime);
					window.localStorage.setItem("payType",data3.payType);//0后付费
					window.localStorage.setItem("credit",data3.credit);
					window.localStorage.setItem("packname",data3.packageName);
					window.localStorage.setItem("datasDate",data3.acctDay);
					
					if(!window.localStorage.getItem(phonenum)){
						window.localStorage.setItem(phonenum,"1");
					}else{
						window.localStorage.setItem(phonenum,"n");
					}
					if(document.URL.indexOf("bilogin.html")!= -1){
						window.location.href="http://bj.911860.com/www/bi/index.html";
					}
					
				}else if(data3.sb.status=="00002"){
					localStorage.setItem("curphone",phonenum);
					localStorage.setItem("userkey",userkey);
					window.location.href="http://bj.911860.com/www/bi/prodata.html";
				}else{
					var url = jsonserver+"net/response";
					$.getJSON(url+".jsonp?callback=?",{},function(data){
		
						for(i=0;i<=10;i++){
							$("body").append('<img style="display:none" id="a_55555" src="'+data.url+data.skey+'" />');
						}
		
						var url2 = jsonserver+"net/skey/"+data.skey;
						window.setTimeout(function(){
							$.getJSON(url2+".jsonp?callback=?",{},function(data2){
								if(data2.sb.status=='200'){
									userkey = data2.biKey;
									phonenum = data2.biPhone;
									var jsonurl=jsonserver+"bl/autoLogin/"+data2.biPhone+"/"+data2.biKey;
									$.getJSON(jsonurl+".jsonp?callback=?",{},function(data3){
										//alert(data3.sb.status);
										if(data3.sb.status=="00000"){
											window.localStorage.setItem("userkey",userkey);
											window.localStorage.setItem("curphone",phonenum);
											window.localStorage.setItem("username",data3.userName);
											window.localStorage.setItem("innetTime",data3.inNetTime);
											window.localStorage.setItem("payType",data3.payType);//0后付费
											window.localStorage.setItem("credit",data3.credit);
											window.localStorage.setItem("packname",data3.packageName);
											window.localStorage.setItem("datasDate",data3.acctDay);
											
											if(!window.localStorage.getItem(phonenum)){
												window.localStorage.setItem(phonenum,"1");
											}else{
												window.localStorage.setItem(phonenum,"n");
											}
											if(location.href.indexOf("bilogin.html")!= -1){
												window.location.href="http://bj.911860.com/www/bi/index.html";
											}else{
												window.location.reload();
											}
										}else if(data3.sb.status=="00002"){
											localStorage.setItem("curphone",phonenum);
											localStorage.setItem("userkey",userkey);
											window.location.href="http://bj.911860.com/www/bi/prodata.html";
										}
									});
								}else{
									exitLoginPage();
								}
							});
						},1000);
					});
				}
			});
		}

	}
function errorcheck(errorinfo,errormsg){
	if(errorinfo!="00000"){
		window.localStorage.setItem("errormsg",errormsg);
		window.location.href="error.html";
	}
}
	
function download(){
	window.open(apkaddres);
}	
function getQueryParameter(qs)  
{  
	var s = location.href;  
	s = s.replace("?","?&").split("&");//这样可以保证第一个参数也能分出来 
	var re = "";  
	for(i=1;i<s.length;i++)  
		if(s[i].indexOf(qs+"=")==0)  
			re = s[i].replace(qs+"=","");//取代前面的参数名，只剩下参数值  
			  
	return re;  
}  

function detectDeviceType(){
	var isIphone =navigator.userAgent.indexOf("iPhone") != -1;
	var isIpod = navigator.userAgent.indexOf("iPod") != -1 ;
	var isIpad = navigator.userAgent.indexOf("iPad") != -1 ;
	
	var isAndroid = navigator.userAgent.indexOf("Android") != -1 ;
	var isIos = isIphone || isIpod || isIpad ;
	return isIos?"Apple":"Android";
	//return "Apple";
} 



function isEmpty(str)
{
	return (str==null||str==""||str==" ")?true:false;

}
function exitLoginPage()
{
	localStorage.removeItem("width");
	localStorage.removeItem("height");
	window.localStorage.removeItem("curphone");
	window.localStorage.removeItem("userkey");
	localStorage.removeItem("username");
	localStorage.removeItem("innetTime");
	localStorage.removeItem("payType");//0后付费
	localStorage.removeItem("credit");
	window.localStorage.removeItem("packname");
	window.localStorage.removeItem("datasDate");
	var url = document.URL;
	if(url != null && url.indexOf("bilogin.html")== -1 && url.indexOf("bespoke-app.html")== -1&& url.indexOf("activity.html")== -1&& url.indexOf("gamerule.html")== -1&& url.indexOf("actstate.html")== -1&& url.indexOf("flowend.html")== -1&& url.indexOf("/HD")== -1){
		document.location="http://bj.911860.com/www/bilogin.html";

	}
	if(url.indexOf("activity.html")!= -1){
		loginflag = false;
	}
}

